package 二叉树题目.二叉树的前序遍历;



import java.util.ArrayList;
import java.util.List;


public class Solution {
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        List<TreeNode> list = new ArrayList<>();
        preorder(root, list);
        int[] ret = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ret[i] = list.get(i).val;
        }
        return ret;

    }
    public void preorder(TreeNode root, List<TreeNode> list) {
        if (root == null) {
            return;
        }
        list.add(root);
        preorder(root.left,list);
        preorder(root.right,list);
    }
}
